/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * <p>https://www.renren.io
 *
 * <p>版权所有，侵权必究！
 */
package io.renren.datasource.config;

import com.alibaba.druid.pool.DruidDataSource;
import io.renren.datasource.properties.DataSourceProperties;
import io.renren.datasource.properties.DynamicDataSourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置多数据源
 *
 * @author Mark sunlightcs@gmail.com
 */
@Configuration
@EnableConfigurationProperties(DynamicDataSourceProperties.class)
public class DynamicDataSourceConfig {
  @Autowired private DynamicDataSourceProperties properties;

  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.druid")
  public DataSourceProperties dataSourceProperties() {
    return new DataSourceProperties();
  }

  @Bean
  public DynamicDataSource dynamicDataSource(DataSourceProperties dataSourceProperties) {
    DynamicDataSource dynamicDataSource = new DynamicDataSource();
    dynamicDataSource.setTargetDataSources(getDynamicDataSource());

    // 默认数据源
    DruidDataSource defaultDataSource =
        DynamicDataSourceFactory.buildDruidDataSource(dataSourceProperties);
    dynamicDataSource.setDefaultTargetDataSource(defaultDataSource);

    return dynamicDataSource;
  }

  private Map<Object, Object> getDynamicDataSource() {
    Map<String, DataSourceProperties> dataSourcePropertiesMap = properties.getDatasource();
    Map<Object, Object> targetDataSources = new HashMap<>(dataSourcePropertiesMap.size());
    dataSourcePropertiesMap.forEach(
        (k, v) -> {
          DruidDataSource druidDataSource = DynamicDataSourceFactory.buildDruidDataSource(v);
          targetDataSources.put(k, druidDataSource);
        });

    return targetDataSources;
  }
}
